//app.js
var WXAPI = require('pages/main/main');
App({
  onLaunch: function () {
    this.setBackGroundColor();
    this.getSystemInfo();
    this.autoUpdate();
  },
  /**设置皮肤***/
  setBackGroundColor() {
    var now = new Date();
    var hours = now.getHours(); // 时
    var bgColor = "morning";
    var hello = "早上好！";
    var appName = this.globalData.appName;
    var helloStr = "美好的一天，从" + appName + "开始！";
    if (hours >= 0 && hours <= 10) {
      bgColor = "morning"; //#2F79B2
      hello = "早上好！";
      helloStr = "美好的一天，从" + appName + "开始！";

    } else if (hours >= 18 && hours <= 24) {
      bgColor = "night"; //#38546D
      hello = "晚上好！";
      helloStr = "最美的旅程，是回家的路！";

    } else {
      bgColor = "work"; //#665DB8
      hello = "你好！";
      helloStr = "性悟长生海，花香万里桥!";
    }
    this.globalData.currentBgColor = bgColor;
    this.globalData.welcomeHello = hello;
    this.globalData.welcomeHelloStr = helloStr;
  },
  //获取系统信息
  getSystemInfo() {
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        this.globalData.SystemWidth = e.windowWidth;
        this.globalData.SystemHeight = e.windowHeight;
        this.globalData.ScreenHeight = e.screenHeight;
        this.globalData.ScreenWidth = e.screenWidth;
        this.globalData.StatusBarHeight = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
  },
  //提示用户自动更新
  autoUpdate: function () {
    console.log(new Date())
    var self = this;
    var appName = this.globalData.appName;
    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      //1. 检查小程序是否有新版本发布
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          //2. 小程序有新版本，则静默下载新版本，做好更新准备
          updateManager.onUpdateReady(function () {
              console.log(new Date())
              wx.showModal({
                title: appName + '温馨提示',
                content: '新版本已经准备好，是否重启应用？',
                success: function (res) {
                  if (res.confirm) {
                    //3. 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                    updateManager.applyUpdate();
                  } else if (res.cancel) {
                    //如果需要强制更新，则给出二次弹窗，如果不需要，则这里的代码都可以删掉了
                    wx.showModal({
                      title: appName + '温馨提示',
                      content: '新版本更新涉及到新的功能添加，旧版本无法正常访问的哦！',
                      success: function (res) {
                        self.autoUpdate()
                        return;
                        //第二次提示后，强制更新           
                        if (res.confirm) {
                          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                          updateManager.applyUpdate();
                        } else if (res.cancel) {
                          //重新回到版本更新提示
                          self.autoUpdate();
                        }
                      }
                    })
                  }
                }
              })
            }),
            updateManager.onUpdateFailed(function () {
              // 新的版本下载失败
              wx.showModal({
                title: '模板又有新版本了哟~',
                content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
              })
            })
        }
      })
    }
  },
  /**设置当前周末**/
  setWeekDay() {
    var that = this;
    var now = new Date();
    var day = now.getDay();
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    this.globalData.currentWeekDay = weeks[day];
  },
  globalData: {
    appName: '赤川小程序模板',
    user: {}, //用户信息
    appid: 'wx208edf1b5900eb33', //appid
    secret: '60131d4348accdd509d378956f21e837', //secret 
    currentBgColor: 'morning', //变色主题
    welcomeHello: '早上好！',
    welcomeHelloStr: '美好的一天，从赤川小程序模板V2.0.0开始！',
    currentWeekDay: '星期一',
    ColorList: [{                                           
        title: '嫣红',
        name: 'red',
        color: '#e54d42'
      },
      {
        title: '桔橙',
        name: 'orange',
        color: '#f37b1d'
      },
      {
        title: '明黄',
        name: 'yellow',
        color: '#fbbd08'
      },
      {
        title: '橄榄',
        name: 'olive',
        color: '#8dc63f'
      },
      {
        title: '森绿',
        name: 'green',
        color: '#39b54a'
      },
      {
        title: '天青',
        name: 'cyan',
        color: '#1cbbb4'
      },
      {
        title: '海蓝',
        name: 'blue',
        color: '#0081ff'
      },
      {
        title: '姹紫',
        name: 'purple',
        color: '#6739b6'
      },
      {
        title: '木槿',
        name: 'mauve',
        color: '#9c26b0'
      },
      {
        title: '桃粉',
        name: 'pink',
        color: '#e03997'
      },
      {
        title: '棕褐',
        name: 'brown',
        color: '#a5673f'
      },
      {
        title: '玄灰',
        name: 'grey',
        color: '#8799a3'
      },
      {
        title: '草灰',
        name: 'gray',
        color: '#aaaaaa'
      },
      {
        title: '墨黑',
        name: 'black',
        color: '#333333'
      },
      {
        title: '雅白',
        name: 'white',
        color: '#ffffff'
      },
    ]
  }
})